package com.whfc.ms.api.mach.controller.v1;

import com.whfc.common.result.ListData;
import com.whfc.common.result.PageData;
import com.whfc.common.result.Result;
import com.whfc.common.result.ResultUtil;
import com.whfc.common.util.DateUtil;
import com.whfc.common.util.SessionAttr;
import com.whfc.entity.dto.OssPathDTO;
import com.whfc.fuum.entity.SysUser;
import com.whfc.mach.dto.*;
import com.whfc.ms.api.mach.service.MsMachDataService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author sun_guodong
 * @description 设备数据接口
 * @date 2020-11-11
 */
@RestController
@RequestMapping("/ms/api/v1/mach")
public class MsMachDataController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private MsMachDataService msMachDataService;

    @GetMapping(value = "/list")
    public Result list(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                       @RequestParam("deptId") Integer deptId,
                       @RequestParam("machType") Integer machType) {
        logger.info("设备列表(不分页),loginer:{},deptId:{},machType:{}", user.getUsername(), deptId, machType);
        ListData<AppMachDTO> pageData = msMachDataService.list(deptId, machType);
        return ResultUtil.success(pageData);
    }

    @GetMapping(value = "/list", params = {"pageNum", "pageSize"})
    public Result list(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                       @RequestParam("deptId") Integer deptId,
                       @RequestParam("pageNum") Integer pageNum,
                       @RequestParam("pageSize") Integer pageSize,
                       @RequestParam(value = "keyword", required = false) String keyword,
                       @RequestParam(value = "machType", required = false) Integer machType,
                       @RequestParam(value = "state", required = false) Integer state,
                       @RequestParam(value = "corpId", required = false) Integer corpId,
                       @RequestParam(value = "bindFlag", required = false) Integer bindFlag,
                       @RequestParam(value = "netState", required = false) Integer netState,
                       @RequestParam(value = "workState", required = false) Integer workState,
                       @RequestParam(value = "machId", required = false) Integer machId) {
        logger.info("设备列表(分页),loginer:{},deptId:{},pageNum:{},pageSize:{},keyword:{},machType:{},state:{},corpId:{},bindFlag:{},netState:{},workState:{},machId:{}",
                user.getUsername(), deptId, pageNum, pageSize, keyword, machType, state, corpId, bindFlag, netState, workState, machId);
        PageData<AppMachDTO> pageData = msMachDataService.list(deptId, pageNum, pageSize, keyword, machType, state, corpId, bindFlag, netState, workState, machId);
        return ResultUtil.success(pageData);
    }

    @GetMapping("/map/list")
    public Result mapList(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                          @RequestParam("deptId") Integer deptId,
                          @RequestParam(value = "corpId", required = false) Integer corpId,
                          @RequestParam(value = "keyword", required = false) String keyword) {
        logger.info("获取地图页设备列表,loginer:{},deptId:{},corpId:{},keyword:{}", user.getUsername(), deptId, corpId, keyword);
        MapMachDTO data = msMachDataService.mapList(deptId, corpId, keyword);
        return ResultUtil.success(data);
    }

    @GetMapping("/numStat")
    public Result numStat(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                          @RequestParam("deptId") Integer deptId,
                          @RequestParam(value = "keyword", required = false) String keyword) {
        logger.info("后台统计设备数据,loginer:{},deptId:{},keyword:{},bindFlag:{},netState:{}",
                user.getUsername(), deptId, keyword);
        MachDataStatDTO data = msMachDataService.numStat(deptId, keyword);
        return ResultUtil.success(data);
    }

    @GetMapping(value = "/data/list", params = {"pageNum", "pageSize"})
    public Result dataList(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                           @RequestParam("deptId") Integer deptId,
                           @RequestParam("pageNum") Integer pageNum,
                           @RequestParam("pageSize") Integer pageSize,
                           @RequestParam(value = "keyword", required = false) String keyword,
                           @RequestParam(value = "bindFlag", required = false) Integer bindFlag,
                           @RequestParam(value = "netState", required = false) Integer netState,
                           @RequestParam(value = "workState", required = false) Integer workState) {
        logger.info("获取设备数据列表,loginer:{},deptId:{},pageNum:{},pageSize:{},keyword:{},bindFlag:{},netState:{},workState:{}",
                user.getUsername(), deptId, pageNum, pageSize, keyword, bindFlag, netState, workState);
        PageData<AppMachDTO> pageData = msMachDataService.dataList(deptId, pageNum, pageSize, keyword, bindFlag, netState, workState);
        return ResultUtil.success(pageData);
    }

    @GetMapping("/data/list")
    public Result dataList(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                           @RequestParam("deptId") Integer deptId,
                           @RequestParam(value = "keyword", required = false) String keyword,
                           @RequestParam(value = "deviceType", required = false) Integer deviceType) {
        logger.info("获取设备数据列表(不分页),loginer:{},deptId:{},keyword:{},deviceType:{}",
                user.getUsername(), deptId, keyword, deviceType);
        ListData<AppMachDTO> listData = msMachDataService.dataList(deptId, keyword, deviceType);
        return ResultUtil.success(listData);
    }

    @GetMapping("/device/list")
    public Result deviceList(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                             @RequestParam("deptId") Integer deptId,
                             @RequestParam("pageNum") Integer pageNum,
                             @RequestParam("pageSize") Integer pageSize,
                             @RequestParam(value = "keyword", required = false) String keyword,
                             @DateTimeFormat(pattern = "yyyy-MM-dd") @RequestParam(value = "startDate", required = false) Date startDate,
                             @DateTimeFormat(pattern = "yyyy-MM-dd") @RequestParam(value = "endDate", required = false) Date endDate) {
        logger.info("机械云管家列表,loginer:{},deptId:{},pageNum:{},pageSize:{},keyword:{},startDate:{},endDate:{}",
                user.getUsername(), deptId, pageNum, pageSize, keyword, startDate, endDate);
        Date startTime = null;
        Date endTime = null;
        if (startDate != null) {
            startTime = DateUtil.getDateBegin(startDate);
        }
        if (endDate != null) {
            endTime = DateUtil.getDateEnd(endDate);
        }
        PageData<AppMachDTO> pageData = msMachDataService.deviceList(deptId, pageNum, pageSize, keyword, startTime, endTime);
        return ResultUtil.success(pageData);
    }

    @GetMapping(value = {"/anon/info", "/info"})
    public Result getMachInfo(@RequestParam("machId") Integer machId) {
        logger.info("查找某一台设备信息,machId:{}", machId);
        AppMachDTO appMachDTO = msMachDataService.getMachInfo(machId);
        return ResultUtil.success(appMachDTO);
    }

    @GetMapping("/getMachQr")
    public Result getMachQr(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                            @RequestParam("machId") Integer machId) {
        logger.info("获取设备二维码,loginer:{},machId:{}", user.getNickname(), machId);
        OssPathDTO data = msMachDataService.getMachQr(machId);
        return ResultUtil.success(data);
    }

    @GetMapping("/gps/log")
    public Result gpsLog(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                         @RequestParam("machId") Integer machId,
                         @DateTimeFormat(pattern = "yyyy-MM-dd") @RequestParam("date") Date date) {
        logger.info("获取设备的运动轨迹,loginer:{},machId:{},date:{}", user.getUsername(), machId, date);
        AppMachGpsDTO data = msMachDataService.gpsLog(machId, date);
        return ResultUtil.success(data);
    }


    /**
     * 查询项目所有设备的运行轨迹
     *
     * @param loginUser
     * @param deptId
     * @param date
     * @return
     */
    @GetMapping("all/gps/list")
    public Result multiMachGpsList(@SessionAttribute(SessionAttr.MS_USER) SysUser loginUser,
                                   @RequestParam("deptId") Integer deptId,
                                   @DateTimeFormat(pattern = "yyyy-MM-dd")
                                   @RequestParam("date") Date date) {
        logger.info("查询项目所有设备gps轨迹,loginUser:{},deptId:{},date:{}", loginUser.getUsername(), deptId, date);
        List<AppMachGpsDTO> gpsList = msMachDataService.getAllMachGpsList(deptId, date);
        return ResultUtil.success(new ListData<>(gpsList));
    }


    /**
     * 查询设备工时
     *
     * @param user
     * @param machId
     * @param date
     * @return
     */
    @GetMapping(value = "/work/data", params = {"machId", "date"})
    public Result getDayWorkData(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                 @RequestParam("machId") Integer machId,
                                 @RequestParam("date") @DateTimeFormat(pattern = "yyyy-MM-dd") Date date) {
        logger.info("查找单台设备一天工时，loginer:{},machId:{},date:{}", user.getUsername(), machId, date);
        MachRunDataDTO machRunDataDTO = msMachDataService.getWorkData(machId, date);
        return ResultUtil.success(machRunDataDTO);
    }

    /**
     * 查询设备工时
     *
     * @param user
     * @param month
     * @param machId
     * @return
     */
    @GetMapping(value = "/work/data", params = {"machId", "month"})
    public Result getMonthWorkData(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                   @RequestParam("machId") Integer machId,
                                   @RequestParam("month") @DateTimeFormat(pattern = "yyyy-MM") Date month) {
        logger.info("查找单台设备一个月工时，loginer:{},machId:{},month:{}", user.getUsername(), machId, month);
        Date startDate = DateUtil.getMonthBegin(month);
        Date endDate = DateUtil.getMonthEnd(month);
        MachRunDataDTO machRunDataDTO = msMachDataService.getMachWorkAndOilData(machId, startDate, endDate);
        return ResultUtil.success(machRunDataDTO);
    }

    /**
     * 查询设备工时
     *
     * @param user
     * @param machId
     * @param startDate
     * @param endDate
     * @return
     */
    @GetMapping(value = "/work/data", params = {"machId", "startDate", "endDate"})
    public Result getCustomWorkData(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                    @RequestParam("machId") Integer machId,
                                    @RequestParam("startDate") @DateTimeFormat(pattern = "yyyy-MM-dd") Date startDate,
                                    @RequestParam("endDate") @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate) {
        logger.info("查找单台设备一周工时，loginer:{},machId:{},startDate:{},endDate:{}", user.getUsername(), machId, startDate, endDate);
        MachRunDataDTO machRunDataDTO = msMachDataService.getMachWorkAndOilData(machId, startDate, endDate);
        return ResultUtil.success(machRunDataDTO);
    }

    /**
     * 查询设备指数
     *
     * @param user
     * @param machId
     * @param date
     * @return
     */
    @GetMapping(value = "/radar/data", params = {"date"})
    public Result getDayRadarData(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                  @RequestParam("machId") Integer machId,
                                  @RequestParam("date") @DateTimeFormat(pattern = "yyyy-MM-dd") Date date) {
        logger.info("查找单台设备单天运行指数，loginer:{},machId:{},date:{}", user.getUsername(), machId, date);
        MachRadarMapDTO machRadarMap = msMachDataService.getRadarData(machId, date, date);
        return ResultUtil.success(machRadarMap);
    }

    /**
     * 查询设备指数
     *
     * @param user
     * @param month
     * @param machId
     * @return
     */
    @GetMapping(value = "/radar/data", params = {"month"})
    public Result getMonthRadarData(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                    @RequestParam("machId") Integer machId,
                                    @RequestParam("month") @DateTimeFormat(pattern = "yyyy-MM") Date month) {
        logger.info("查找单台设备一个月运行指数，loginer:{},machId:{},month:{}", user.getUsername(), machId, month);
        Date startDate = DateUtil.getMonthBegin(month);
        Date endDate = DateUtil.getMonthEnd(month);
        MachRadarMapDTO machRadarMap = msMachDataService.getRadarData(machId, startDate, endDate);
        return ResultUtil.success(machRadarMap);
    }

    /**
     * 查询设备指数
     *
     * @param user
     * @param machId
     * @param startDate
     * @param endDate
     * @return
     */
    @GetMapping(value = "/radar/data", params = {"startDate", "endDate"})
    public Result getCustomRadarData(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                     @RequestParam("machId") Integer machId,
                                     @RequestParam("startDate") @DateTimeFormat(pattern = "yyyy-MM-dd") Date startDate,
                                     @RequestParam("endDate") @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate) {
        logger.info("查找单台设备多天运行指数，loginer:{},machId:{},startDate:{},endDate:{}", user.getUsername(), machId, startDate, endDate);
        MachRadarMapDTO machRadarMap = msMachDataService.getRadarData(machId, startDate, endDate);
        return ResultUtil.success(machRadarMap);
    }


    /**
     * 查询设备油耗
     *
     * @param user
     * @param machId
     * @param date
     * @return
     */
    @GetMapping(value = "/oil/data", params = {"date"})
    public Result getOilData(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                             @RequestParam("machId") Integer machId,
                             @RequestParam("date") @DateTimeFormat(pattern = "yyyy-MM-dd") Date date) {
        logger.info("查找单台设备每天油耗，loginer:{},machId:{},date:{}", user.getUsername(), machId, date);
        MachRunDataDTO machRunDataDTO = msMachDataService.getOilData(machId, date);
        return ResultUtil.success(machRunDataDTO);
    }


    /**
     * 查询设备油耗
     *
     * @param user
     * @param machId
     * @param startDate
     * @param endDate
     * @return
     */
    @GetMapping(value = "/oil/data", params = {"startDate", "endDate"})
    public Result getOilData(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                             @RequestParam("machId") Integer machId,
                             @RequestParam("startDate") @DateTimeFormat(pattern = "yyyy-MM-dd") Date startDate,
                             @RequestParam("endDate") @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate) {
        logger.info("查找单台设备多天油耗，loginer:{},machId:{},startDate:{},endDate:{}", user.getUsername(), machId, startDate, endDate);
        MachRunDataDTO machRunDataDTO = msMachDataService.getOilData(machId, startDate, endDate);
        return ResultUtil.success(machRunDataDTO);
    }


    /**
     * 查询设备油耗
     *
     * @param user
     * @param machId
     * @param month
     * @return
     */
    @GetMapping(value = "/oil/data", params = {"month"})
    public Result getMonthOilData(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                  @RequestParam("machId") Integer machId,
                                  @RequestParam("month") @DateTimeFormat(pattern = "yyyy-MM") Date month) {
        logger.info("查找单台设备多天油耗，loginer:{},machId:{},month:{}", user.getUsername(), machId, month);
        Date startDate = DateUtil.getMonthBegin(month);
        Date endDate = DateUtil.getMonthEnd(month);
        MachRunDataDTO machRunDataDTO = msMachDataService.getOilData(machId, startDate, endDate);
        return ResultUtil.success(machRunDataDTO);
    }

    @GetMapping("/dayLog")
    public Result dayLog(@SessionAttribute(SessionAttr.MS_USER) SysUser loginUser,
                         @RequestParam("deptId") Integer deptId,
                         @RequestParam("date") @DateTimeFormat(pattern = "yyyy-MM-dd") Date date) {
        logger.info("查询设备日志,loginer:{},date:{}", loginUser.getUsername(), date);
        MachLogNumDTO data = msMachDataService.dayReportDate(deptId, date);
        return ResultUtil.success(data);
    }

}
